package cc.orange.mainView;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import cc.orange.adapter.MsgaImgAdapter;
import cc.orange.databinding.FragmentMsgInfoBinding;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import cc.orange.adapter.HomeAdapter;
import cc.orange.base.BaseFragment;
import cc.orange.entity.MsgInfosVideoEntity;
import cc.orange.entity.MsgaIntroduceEntity;
import cc.orange.entity.ReturnImageEntity;
import cc.orange.entity.ReturnWmEntity;
import cc.orange.http.ApiService;

import cc.orange.R;

import cc.orange.utils.ZXToastUtil;
import cc.orange.utils.flowlayouts.TagAdapter;
import cc.orange.utils.flowlayouts.TagFlowLayout;

public class MsgInfoFragment extends BaseFragment {

    private FragmentMsgInfoBinding binding;
    private MsgaImgAdapter adapter;
    private ArrayList<String> list = new ArrayList<>();
    private TagAdapter mRecordsAdapter;
    private List<String> recordList = new ArrayList<>();//历史记录

    private String uid;
    public MsgInfoFragment(String uid){
        this.uid = uid;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_msg_info,container,false);
        initViews();
//        returnVideo();
        returnImag();
        returnWoMan();
        return binding.getRoot();
    }

    private void initViews() {
//        binding.homeHfText2.setInAnimation(R.anim.animation_down_up_in_animation)
//                .setOutAnimation(R.anim.animation_down_up_out_animation)
//                .bindData(list_str.toArray(new String[0]))
//                .startSwitch(5000);
//        GridLayoutManager gridLayoutManagers = new GridLayoutManager(getContext(), 4, GridLayoutManager.VERTICAL, false);
//        recyclerView.setLayoutManager(gridLayoutManager);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 4);
        binding.msgInfoRecycler.setLayoutManager(layoutManager);
        adapter = new MsgaImgAdapter(list,2);
        binding.msgInfoRecycler.setAdapter(adapter);


//        WrappingGridLayoutManager gridLayoutManager = new WrappingGridLayoutManager(getContext(), 2, GridLayoutManager.VERTICAL, false);
//        binding.msgInfoRecycler.setLayoutManager(gridLayoutManager);
//        binding.msgInfoRecycler.setHasFixedSize(true);//固定自身size不受adapter变化影响
//        //消除滑动卡顿现象 ...
//        binding.msgInfoRecycler.setNestedScrollingEnabled(false);//限制recyclerview自身滑动特性,滑动全部靠scrollview完成


        //下拉刷新
        binding.smartRL.setRefreshHeader(new ClassicsHeader(getActivity()));
        binding.smartRL.setRefreshFooter(new ClassicsFooter(getActivity()));
        binding.smartRL.setEnableLoadMore(true);
        binding.smartRL.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                binding.smartRL.finishRefresh(3);
            }
        });
        binding.smartRL.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                binding.smartRL.finishLoadMore(3);
            }
        });

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Intent intent = new Intent(getActivity(), VideoActivity.class);
//                startActivity(intent);
            }
        });

        //为标签设置对应的内容
        mRecordsAdapter = new TagAdapter<String>(recordList) {

            @Override
            public View getView(TagFlowLayout parent, int position, String s) {
                TextView tv = new TextView(getActivity());
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(9, 10, 9, 5);
                tv.setPadding(26, 16, 26, 16);
                tv.setBackground(getResources().getDrawable(R.drawable.shape_search_history));
                tv.setLayoutParams(params);
                tv.setLines(1);
                tv.setTextColor(getResources().getColor(R.color.black));
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
                //为标签设置对应的内容
                tv.setText(s);
                return tv;
            }
        };

        binding.searchHistorys.setAdapter(mRecordsAdapter);
    }

    private void returnVideo(){
        RetrofitFactory.create(ApiService.class)
                .returnVideo(getTokens(),uid)
                .enqueue(new DefaultCallback<MsgInfosVideoEntity>() {
                    @Override
                    public void onStart(Call<MsgInfosVideoEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<MsgInfosVideoEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<MsgInfosVideoEntity> call, MsgInfosVideoEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
//                            list.addAll(baseEntity.getData());
//                            adapter.notifyDataSetChanged();
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }


    @Override
    public void onResume() {
        super.onResume();
        TalkingDataSDK.onPageBegin(getActivity().getApplicationContext(), this.toString());
    }

    @Override
    public void onPause() {
        if(!this.isHidden()){
            TalkingDataSDK.onPageEnd(getActivity().getApplicationContext(), this.toString());
        }
        super.onPause();
    }

    private  void returnWoMan(){
        RetrofitFactory.create(ApiService.class)
                .returnWoMan(getTokens(),uid)
                .enqueue(new DefaultCallback<ReturnWmEntity>() {
                    @Override
                    public void onStart(Call<ReturnWmEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<ReturnWmEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<ReturnWmEntity> call, ReturnWmEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            recordList.add(""+baseEntity.getData().getAge());
                            recordList.add(""+baseEntity.getData().getWork());
                            try{
                                String[] hobby = baseEntity.getData().getHobby().split(",");
                                for (int i = 0; i < hobby.length; i++) {
                                    recordList.add(hobby[i]);
                                }
                            }catch (Exception e){
                                recordList.add(baseEntity.getData().getHobby());
                            }
                            mRecordsAdapter.notifyDataChanged();

                            EventBus.getDefault().post(new MsgaIntroduceEntity(baseEntity.getData().getIntroduce()));
                        }else{

                        }
                    }
                });
    }

    private  void returnImag(){
        RetrofitFactory.create(ApiService.class)
                .returnImag(getTokens(),uid)
                .enqueue(new DefaultCallback<ReturnImageEntity>() {
                    @Override
                    public void onStart(Call<ReturnImageEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<ReturnImageEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<ReturnImageEntity> call, ReturnImageEntity baseEntity) {
                        cancelLoading();
                        for (int i = 0; i < baseEntity.getData().size(); i++) {
                            list.add(baseEntity.getData().get(i).getImage());
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
    }
}